package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AssesManagement;
import com.ruoyi.system.domain.AssesRule;
import com.ruoyi.system.domain.PointsManagement;
import com.ruoyi.system.domain.PointsRule;
import com.ruoyi.system.service.IAssesManagementService;
import com.ruoyi.system.service.IAssesRuleService;
import com.ruoyi.system.service.IPointsManagementService;
import com.ruoyi.system.service.IPointsRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 考核管理Controller
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/system/asses/management")
public class AssesManagementController extends BaseController
{
    @Autowired
    private IAssesManagementService assesManagementService;
    @Autowired
    private IAssesRuleService assesRuleService;

    /**
     * 查询考核管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody AssesManagement assesManagement)
    {
        setStartPage(assesManagement.getPageNum(),assesManagement.getPageSize());
        List<AssesManagement> list = assesManagementService.selectAssesManagementList(assesManagement);
        return getDataTable(list);
    }

    /**
     * 获取考核管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:management:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(assesManagementService.selectAssesManagementById(id));
    }

    /**
     * 新增考核管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:add')")
    @Log(title = "考核管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AssesManagement assesManagement)
    {
        assesManagement.setCreateBy(String.valueOf(getUserId()));
        assesManagement.setCreateName(getUsername());
        return toAjax(assesManagementService.insertAssesManagement(assesManagement));
    }

    /**
     * 修改考核管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:edit')")
    @Log(title = "考核管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody AssesManagement assesManagement)
    {
        return toAjax(assesManagementService.updateAssesManagement(assesManagement));
    }

    /**
     * 删除考核管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:remove')")
    @Log(title = "考核管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(assesManagementService.deleteAssesManagementById(id));
    }

    /**
     * 审核
     */
    @PreAuthorize("@ss.hasPermi('system:management:edit')")
    @Log(title = "考核管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody AssesManagement assesManagement)
    {
        return toAjax(assesManagementService.updateAssesManagementStatus(assesManagement));
    }

    /**
     * 按照年度查询个人考核统计
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/selectRankByYear")
    public TableDataInfo selectRankByYear(@RequestBody AssesManagement assesManagement)
    {
        String queryYear = assesManagement.getQueryYear();
        String effectiveTerm = queryYear;
        if(StringUtils.isBlank(queryYear)){
            String nowYear = DateUtils.getNowYear();
            assesManagement.setQueryYear(nowYear);
            assesManagement.setStartTime(nowYear+"-01-01 00:00:00");
            assesManagement.setEndTime(nowYear+"-12-31 23:59:59");
        }else{
            assesManagement.setStartTime(queryYear+"-01-01 00:00:00");
            assesManagement.setEndTime(queryYear+"-12-31 23:59:59");
        }
        return assesManagementService.selectRank(assesManagement,effectiveTerm);
    }

    /**
     * 按照月度查询个人考核统计
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/selectRankByMonth")
    public TableDataInfo selectRankByMonth(@RequestBody AssesManagement assesManagement)
    {
        String queryMonth = assesManagement.getQueryMonth();
        String effectiveTerm = queryMonth.substring(0,4);
        if(StringUtils.isBlank(queryMonth)){
            String monthFirtDay = DateUtils.getMonthFirtDay();
            String monthLastDay = DateUtils.getMonthLastDay();
            assesManagement.setStartTime(monthFirtDay+" 00:00:00");
            assesManagement.setEndTime(monthLastDay+" 23:59:59");
        }else{
            String firstDayOfMonth = DateUtils.getFirstDayOfMonth(queryMonth);
            String lastDayOfMonth = DateUtils.getLastDayOfMonth(queryMonth);
            assesManagement.setStartTime(firstDayOfMonth+" 00:00:00");
            assesManagement.setEndTime(lastDayOfMonth+" 23:59:59");
        }
        return assesManagementService.selectRank(assesManagement,effectiveTerm);
    }
}
